//
//  MKPhotosViewController.swift
//  MKPhotoDetailDemo
//
//  Created by 劢克科技 on 2019/3/15.
//  Copyright © 2019 劢克科技. All rights reserved.
//

import UIKit

class MKPhotosViewController: UIViewController {
    
    var image: UIImageView!
    
    fileprivate lazy var cover: UIView = {
        let cover = UIView()
        cover.backgroundColor = UIColor.black
        return cover
    }()
    fileprivate lazy var browseImage = UIImageView()
    fileprivate var beginPoint: CGPoint?
    fileprivate var currentLocation: CGPoint?
    fileprivate var shouldHide = false
}

//MARK: - Life Circle

extension MKPhotosViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.clear
        setViewGesture()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        showAnimation()
    }
}

//MARK: - Actions

extension MKPhotosViewController {
    
    fileprivate func setViewGesture() {
        let leftGesture = UISwipeGestureRecognizer(target: self, action: nil)
        let rightGesture = UISwipeGestureRecognizer(target: self, action: nil)
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handleGesture(gesture:)))
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(gesture:)))
        leftGesture.direction = .left
        rightGesture.direction = .right
        panGesture.require(toFail: leftGesture)
        panGesture.require(toFail: rightGesture)
        tapGesture.require(toFail: panGesture)
        view.addGestureRecognizer(leftGesture)
        view.addGestureRecognizer(rightGesture)
        view.addGestureRecognizer(panGesture)
        view.addGestureRecognizer(tapGesture)
    }
    
    @objc fileprivate func handleTapGesture(gesture: UITapGestureRecognizer) {
        hideAnimation()
    }
    
    @objc fileprivate func handleGesture(gesture: UIPanGestureRecognizer) {
        let location = gesture.location(in: view)
        if gesture.state == .began {
            beginPoint = location
        } else if gesture.state == .changed {
            let offsetX = location.x - beginPoint!.x
            let offsetY = location.y - beginPoint!.y
            let percent = 1 - abs(offsetY) / UIScreen.main.bounds.height

            browseImage.transform = CGAffineTransform(translationX: offsetX, y: offsetY)
            browseImage.transform = browseImage.transform.scaledBy(x: percent, y: percent)
            cover.alpha = percent
            if currentLocation != nil {
                if offsetY > 0 {
                    shouldHide = currentLocation!.y < location.y
                } else {
                    shouldHide = currentLocation!.y > location.y
                }
            } else {
                shouldHide = false
            }
            currentLocation = location
        } else if gesture.state == .ended {
            if shouldHide == true {
                hideAnimation()
            } else {
                UIView.animate(withDuration: 0.25) {
                    self.browseImage.transform = .identity
                    self.cover.alpha = 1
                }
            }
        }
    }
    
    fileprivate func addCover() {
        cover.alpha = 0
        cover.frame = view.bounds
        view.addSubview(cover)
    }
    
    fileprivate func setCoverStyle() {
        cover.alpha = 1
    }
    
    fileprivate func removeCoverStyle() {
        cover.alpha = 0
    }
    
    fileprivate func addBrowseImage() {
        browseImage.image = image.image
        browseImage.frame = convertOriginFrame(frame: image.frame)
        browseImage.contentMode = image.contentMode
        browseImage.clipsToBounds = true
        view.addSubview(browseImage)
    }
    
    fileprivate func setBrowseImageStyle() {
        let x: CGFloat = 0
        let w = UIScreen.main.bounds.width
        let h = (image.image?.size.height)! / (image.image?.size.width)! * UIScreen.main.bounds.width
        var y = (UIScreen.main.bounds.height - h) / 2
        y = y < 0 ? 0 : y
        browseImage.frame = CGRect(x: x, y: y, width: w, height: h)
    }
    
    fileprivate func convertOriginFrame(frame: CGRect) -> CGRect {
        let originFrame = image.superview!.convert(frame, from: UIApplication.shared.keyWindow)
        return CGRect(x: abs(originFrame.origin.x), y: abs(originFrame.origin.y), width: originFrame.width, height: originFrame.height)
    }

    fileprivate func removeBrowserImageStyle() {
        browseImage.frame = convertOriginFrame(frame: image.frame)
    }
    
    fileprivate func showAnimation() {
        addCover()
        addBrowseImage()
        
        UIView.animate(withDuration: 0.25, animations: {
            self.setCoverStyle()
            self.setBrowseImageStyle()
        }) { (_) in
            // 动画完成
        }
    }
    
    fileprivate func hideAnimation() {
        UIView.animate(withDuration: 0.25, animations: {
            self.removeCoverStyle()
            self.removeBrowserImageStyle()
        }) { (_) in
            self.dismiss(animated: false, completion: {
                // 窗口关闭
            })
        }
    }
}
